namespace Unosquare.Labs.Scratchpad
{
    using System;
    using Microsoft.SPOT;
    using Unosquare.Labs.Drivers.Vizic;

    /// <summary>
    /// Shows how to implement a very simple scene loop.
    /// Much like game or animation scene, the loop 
    /// captures the input, updates objects, clears the screen
    /// and draws all the objects.
    /// The down-side is that the LCD is a little flickerish
    /// so it does not look great.
    /// Idea for the Vizic people: Add a command to retain the screen buffer
    /// and another command to commit the buffer to the screen. This would
    /// effectively reduce or eliminate flickering.
    /// </summary>
    public class SampleScene : VizicSceneBase
    {

        static public void RunSample(VizicBaudRates baudRate)
        {
            var lcd = new VizicSmartGpu();
            lcd.BaudRate = baudRate;
            var scene = new SampleScene(lcd);
            scene.Start();
        }

        private const int LineHeight = 12;

        public SampleScene(VizicSmartGpu lcd)
            : base(lcd)
        {
            this.SceneObjects = new VizicSceneObjectBase[1];
            var obj = new VizicRectangle(this);
            obj.Width = 15;
            obj.Height = 15;
            this.SceneObjects[0] = obj;
        }

        private void PerformInput()
        {
            var input = this.LCD.TouchGetData();

            if (input.ResponseType == VizicTouchResponseTypes.PositionData)
            {
                if (input.Y > 120)
                    this.MsPerFrame -= 1;
                else
                    this.MsPerFrame += 1;

            }

            if (input.ResponseType == VizicTouchResponseTypes.TelephoneIcon)
                this.LCD.Brightness -= 1;

            if (input.ResponseType == VizicTouchResponseTypes.SongIcon)
                this.LCD.Brightness += 1;
        }

        protected override void UpdateObjects()
        {

            this.PerformInput();

            this.SceneObjects[0].X += 1; // (ushort)System.Math.Max(this.Elapsed / 24, 1);
            if (this.SceneObjects[0].X > LCD.HorizontalResolution - this.SceneObjects[0].Width)
                this.SceneObjects[0].X = 0;

            this.SceneObjects[0].Y += 1; // (ushort)System.Math.Max(this.Elapsed / 24, 1);
            if (this.SceneObjects[0].Y > LCD.VerticalResolution - this.SceneObjects[0].Width)
                this.SceneObjects[0].Y = 0;
        }

        protected override void DrawUI()
        {
            //return;
            // Setup the writing mode
            this.LCD.ForegroundColor = VizicColor.Yellow;
            this.LCD.BackgroundColor = VizicColor.Black;
            this.LCD.FillBackground = true;

            // write the status lines
            var status = "MPF: " + MsPerFrame + " | " + "LAG: " + (IsLagging ? "Y" : "N") + " | " + "MS: " + Elapsed + " | " + "BR: " + LCD.Brightness;
            this.LCD.WriteString(0, 0, (ushort)(this.LCD.HorizontalResolution), LineHeight, VizicFontSizes.Size10, status);
        }

    }

}
